﻿function changeItem(obj)
{
    
    var sp = document.getElementById("cart");

    var myString = sp.value.toString();
    var mySplitResult = myString.split(",");
    if (obj.checked == true)
    {
        myString += obj.value + ",";
       // alert(sp.value);
    }
    if (obj.checked == false) {
        myString = "";
        for (var i = 0 ; i < mySplitResult.length -1 ; i++) {

            if (obj.value != mySplitResult[i]) { myString += mySplitResult[i]+","; }
        }

        //alert(sp.value);
    }
    sp.value = myString;
   // alert(sp.value);
}

function checkItem(id) {

    var sp = document.getElementById("cart");

    var myString = sp.value.toString();

    var mySplitResult = myString.split(",");

    for (var i = 0 ; i < mySplitResult.length ; i++) {

        if (id == mySplitResult[i]) return true;
    }
    return false;
}


function Sum() {

    // Lay ve 
    //alert(object.value);

    var soluongsp = document.getElementById("count");
    //alert(soluongsp.value);
    var sum=0;
    var price;
    var sl;

    for (var i = 0; i < soluongsp.value ; i++) {

        //alert(soluongsp.value);
        price = document.getElementById("sp" + i);
         sl = document.getElementById("sl" + i);

       // alert(price.value);
        sum += (price.value * sl.value);


    }
    tongtien = document.getElementById("tongtien");
    sum = convertToCurency(sum);
    tongtien.innerHTML = sum + " VNĐ";
    
    

}
function convertToCurency(money) {
    
    var m = money.toString();
    var convert="";
    var count = 0;
   
    for (var i = m.length-1; i >= 0; i--)
    {
                
               convert = (convert +  m[i].toString());
               count++;
               if (count == 3 && i!=0)
                {
                   convert = convert + ".";
                   count = 0;
               }
        
    }
    
   //alert( (convert) );
   return revert(convert.toString());
}
function revert(s) {
    var m = s.toString();
    var temp = "";
    for (var i = m.length - 1; i >= 0; i--)
    {
        temp = (temp + m[i].toString());
    }
   return temp;
}